package com.rongchuang.util;

import com.rongchuang.exception.CustomException;

import java.security.MessageDigest;

public class PasswordEncryptor {

    /**
     * 密码加密
     * @param password 原始密码
     * @return 加密后的密码
     */
    public static String encryptPassword(String password) {
        try {
            // 获取SHA-256实例
            MessageDigest md = MessageDigest.getInstance("SHA-256");

            // 传入密码的字节进行加密
            byte[] hashedBytes = md.digest(password.getBytes());

            // 将字节数组转换成16进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : hashedBytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (Exception e) {
            throw new CustomException(-1, "加密失败", e);
        }
    }
}
